const db = uniCloud.database()
const dbCmd = db.command;
module.exports = {
	main: async (event) => {
		let {
			res,
			data,
			action
		} = event;
		switch (action) {
			case 'query':
				res.data = await getData(data);
				break;
			case 'edit':
				res = await editData(data);
				break;
			default:
				res = {
					code: 402,
					msg: '非法访问'
				}
				break;
		}
		return res;
	}
}

/**
 * 获取列表数据
 */
async function getData(data) {
	const result = await db.collection('setting').get()
	return result.data
}
/**
 * 更新数据
 */
async function editData(data) {
	var res = {}
	const result = await db.collection('setting').doc(data.id).update({
		data: data.data,
		updatetime: data.updatetime
	})
	res.data = result
	if (result.updated === 1) {
		res.code = 200;
		res.data = '更新数据成功';
	} else {
		res.code = 402;
		res.msg = '保存失败，请重新操作'
	}
	return res;
}
